const jwt = require("jsonwebtoken");
// token秘钥
const key = "mcyz yyds";
// token有效时间一小时
const exp = Date.now() / 1000 + 60 * 60;
function getToken(data) {
  let payload = {
    ...data,
    exp,
  };
  let token_str = jwt.sign(payload, key);
  return token_str;
}

function verify(token, callback) {
  jwt.verify(token, key, (err, decoded) => {
    if (err) {
      console.warn(err);
      return;
    }
    callback(decoded);
  });
}

module.exports = { getToken, verify };
